package com.youthfilmic.yuejian.weixin.core;

import com.youthfilmic.yuejian.dao.UserDao;
import com.youthfilmic.yuejian.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * Created by youweixi on 15/10/8.
 */
@Controller
public class WeixinCoreController {
    @Resource
    private UserDao userDao;

    /**
     * 获取url的签名
     *
     * */
    @RequestMapping(value="/weixin/ajax/getWeixinSign.ajax" , method= RequestMethod.POST)
    @ResponseBody
    public WeixinSignBean getSignBean(
            @RequestParam("url") String url
    ){
        return WeixinSign.sign(url);
    }

    /**
     * @instruction 获取用户关注状态
     * @return status : -2 用户没有登录  -1用户不是微信用户    0获取成功
     * 			subscribe 0 没有关注    1已经关注     -1不是微信用户（放行）
     * @author Dxywx
     * @time 2015.4.24
     * */
    @RequestMapping(value="/weixin/user/getSubscribeStatus")
    @ResponseBody
    public SubscribeAjaxResult getSubscribeStatus(
            HttpSession session
    ){
        Long userId = (Long)session.getAttribute("userId");
        User user = userDao.findOne(userId);
        SubscribeAjaxResult result = new SubscribeAjaxResult();
        if (user == null){
            result.setMsg("notLogin");
            result.setStatus(-2);
            result.setSubscribe(0);
        }else{
            if (user.getOpenId()==null){
                result.setMsg("notWeixinUser");
                result.setStatus(-1);
                result.setSubscribe(-1);
            }else{
                boolean t = WeixinAPI.getUserSubscribe(user.getOpenId());
                if (!t){
                    result.setMsg("notSubscribe");
                    result.setStatus(0);
                    result.setSubscribe(0);
                }else{
                    result.setMsg("subscribe");
                    result.setStatus(0);
                    result.setSubscribe(1);
                }
            }
        }
        return result;
    }


}
